package com.yikaiye.android.yikaiye.ui.fragment;

import static com.yikaiye.android.yikaiye.utils.CommonUtil.getNewContent;

import android.os.Bundle;

import androidx.core.widget.NestedScrollView;

import com.google.android.material.tabs.TabLayout;
import com.yikaiye.android.yikaiye.base.BaseFragment;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.FragmentDetailsBinding;
import com.yikaiye.android.yikaiye.model.ProductDesModel;

/**
 * 产品详情-详情
 */
public class DetailsFragment extends BaseFragment<FragmentDetailsBinding, BaseViewModel> {

    private int tabIndex = 0;
    /**
     * 标记是否是scrollview在滑动
     */
    private boolean scrollviewFlag = false;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void initData() {
        if (getArguments() != null) {
            ProductDesModel productDesModel = getArguments().getParcelable(Constant.DETAIL_MODEL_KEY);
            if (null != productDesModel) {
                binding.web1.loadDataWithBaseURL(null, getNewContent(productDesModel.getDetail()), "text/html", "utf-8", null);
                binding.web2.loadDataWithBaseURL(null, getNewContent(productDesModel.getProblem()), "text/html", "utf-8", null);
                binding.web3.loadDataWithBaseURL(null, getNewContent(productDesModel.getGuarantee()), "text/html", "utf-8", null);
            }
        }
    }

    @Override
    protected void initClick() {
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (!scrollviewFlag) {
                    switch (tab.getPosition()) {
                        case 0://scrollview移动到tv1的顶部坐标处
                            binding.scrollView.scrollTo(0, binding.web1.getTop());
                            binding.tabLayout.selectTab(binding.tabLayout.getTabAt(0));
                            break;
                        case 1://scrollview移动到tv2的顶部坐标处
                            binding.scrollView.scrollTo(0, binding.web2.getTop());
                            binding.tabLayout.selectTab(binding.tabLayout.getTabAt(1));
                            break;
                        case 2://scrollview移动到tv2的顶部坐标处
                            binding.scrollView.scrollTo(0, binding.web3.getTop());
                            binding.tabLayout.selectTab(binding.tabLayout.getTabAt(2));
                            break;
                    }
                }
                //用户点击tablayout时，标记不是scrollview主动滑动
                scrollviewFlag = false;
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        binding.scrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            scrollviewFlag = true;
            tabIndex = binding.tabLayout.getSelectedTabPosition();
            if (scrollY < binding.web2.getTop()) {
                // 增加判断，如果滑动的区域是tableIndex=0对应的区域，则不改变tablayout的状态
                if (tabIndex != 0) {
                    binding.tabLayout.selectTab(binding.tabLayout.getTabAt(0));
                }
            } else if (scrollY >= binding.web2.getTop() && scrollY < binding.web3.getTop()) {
                if (tabIndex != 1) {
                    binding.tabLayout.selectTab(binding.tabLayout.getTabAt(1));
                }
            } else if (scrollY >= binding.web3.getTop()) {
                if (tabIndex != 2) {
                    binding.tabLayout.selectTab(binding.tabLayout.getTabAt(2));
                }
            }
            scrollviewFlag = false;
        });
    }

    public static DetailsFragment newInstance(ProductDesModel productDesModel) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(Constant.DETAIL_MODEL_KEY, productDesModel);
        DetailsFragment fragment = new DetailsFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
